package com.xfgg.cloud.boot.config.es;

import com.google.common.base.Splitter;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author xfgg
 */
@Configuration
public class ElasticSearchConfig {
    @Value("${es.addresses}")
    private String esAddresses;

    @Bean
    public RestHighLevelClient restRankingHighLevelClient() {
        if (esAddresses == null) {
            throw new IllegalArgumentException("es地址配置错误");
        }


        HttpHost[] httpHosts = Splitter.on(",").splitToStream(esAddresses)
                .map(address -> {
                    String[] split = address.split(":");
                    return new HttpHost(split[0], Integer.parseInt(split[1]));
                }).toArray(HttpHost[]::new);

        return new RestHighLevelClient(RestClient.builder(httpHosts));

    }
}
